<template>
  <el-dialog
    title="编辑客户"
    :visible="dialogVisible"
    width="50%"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="140px">
      <el-form-item label="经纪">
        <el-select v-model="formData.agent" style="width:50%" placeholder="请选择">
          <el-option v-for="item in Depts" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="name" label="姓名">
        <el-input v-model="formData.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="formData.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="微信">
        <el-input v-model="formData.wechat" placeholder="请输入微信号" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="formData.desc" type="textarea" size="small" placeholder="请输入个人描述" />
      </el-form-item>
      <el-form-item label="封面">
        <UploadFile ref="img" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-col :span="6">
        <el-button size="small" @click="closeFn">取消</el-button>
        <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
      </el-col>
    </el-row>

  </el-dialog>
</template>
<script>
import { getBroker, addClientLists } from '@/api/client'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      showDepts: false,
      Depts: [],
      formData: {
        agent: '',
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [{ required: true, message: '名字不能为空', trigger: 'blur' }, {
          min: 2, max: 5, message: '名字长度2-5', trigger: 'blur'
        }]
      }
    }
  },
  async created () {
    // 获取经纪列表
    const res = await getBroker()
    console.log(73, res)
    this.Depts = res
  },
  methods: {
    confirmFn() {
      // 获取组件内的图片文件
      const obj = this.$refs.img.fileList[0]
      // 将图片地址赋值给表单
      this.formData.image = obj.url
      // 表单校验
      this.$refs.form.validate()
      addClientLists(this.formData)
      this.closeFn()
      this.$emit('addEvent')
    },
    closeFn() {
      // 关闭显示窗，清空表单和校验规则
      this.dialogVisible = false
      this.formData = {
        agent: '',
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      }
      this.$refs.form.resetFields()
    }
  }

}
</script>

<style>

</style>
